package com.exam.service.impl;

import com.exam.dao.ProductDao;
import com.exam.service.ProductService;
import com.exam.util.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    ProductDao productDao;

    @Override
    public List<Map<String, Object>> getProductList(HttpServletRequest request) {
        String org_id = "1";
        List<Map<String, Object>> typeList = productDao.getProductTypeList(org_id);
        List<Map<String, String>> productList = productDao.getProductList(org_id);
        for(Map<String, Object> typeMap : typeList) {
            String typeId = MapUtils.getStringValueByKey(typeMap, "id");
            List<Map<String, String>> tmpList = new ArrayList<>();
            for(Map<String, String> productMap : productList) {
                String type_id = MapUtils.getStringValueByKey(productMap, "type_id");
                if(type_id.equals(typeId)) {
                    tmpList.add(productMap);
                }
            }
            typeMap.put(typeId, tmpList);
        }
        return typeList;
    }
}
